package com.zhongxi.guoyuan.view.seller.offline;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.utils.UiUtils;
import com.zhongxi.guoyuan.view.seller.offline.mode.OrderListBean;
import com.zhongxi.guoyuan.widget.screenadaptaion.CustomLinearlayout;

import org.json.JSONObject;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/1/23.
 */

public class SellerOffLineAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_FOOTER = 0;
    private static final int TYPE_ITEM = 1;
    public static final int LOADING = 0206;
    public static final int LOADCOMPLENT = 0205;
    public static final int LOADEND = 0204;
    private List<OrderListBean> lineList;
    private JSONObject user;
    private int loadingState;

    public SellerOffLineAdapter(List<OrderListBean> orderInfo) {
        this.lineList = orderInfo;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == lineList.size()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_FOOTER) {
            View mInflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.loadmore_footer, parent, false);
            return new FooterViewHolder(mInflate);
        } else {
            return new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_offline_seller, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof MyViewHolder) {
            if (position % 2 == 0) {
                ((MyViewHolder) holder).view.setBackgroundResource(R.color.white);
            } else {
                ((MyViewHolder) holder).view.setBackgroundResource(R.color.background);
            }
            OrderListBean mOrderInfoBean = lineList.get(position);
            ((MyViewHolder) holder).mTvDateline.setText(mOrderInfoBean.ctime);
            ((MyViewHolder) holder).mTvOrderId.setText(mOrderInfoBean.id);
            ((MyViewHolder) holder).mTvPhone.setText(user.optString(mOrderInfoBean.userid));
            ((MyViewHolder) holder).mTvMoney.setText(mOrderInfoBean.shouldprice + "元");
        } else if (holder instanceof FooterViewHolder) {
            switch (loadingState) {
                case LOADING:  // 正在加载
                    ((FooterViewHolder) holder).mLoading.setVisibility(View.VISIBLE);
                    ((FooterViewHolder) holder).mLoadComplent.setVisibility(View.GONE);
                    break;
                case LOADCOMPLENT:  // 加载完成
                    ((FooterViewHolder) holder).mLoading.setVisibility(View.INVISIBLE);
                    ((FooterViewHolder) holder).mLoadComplent.setVisibility(View.GONE);
                    break;
                case LOADEND:  //  记载到底
                    ((FooterViewHolder) holder).mLoading.setVisibility(View.GONE);
                    ((FooterViewHolder) holder).mLoadComplent.setVisibility(View.VISIBLE);
                    break;
            }
        }
    }

    @Override
    public int getItemCount() {
        if (lineList.size() > 0) {
            return lineList.size() + 1;
        }
        return 0;
    }

    public void addUserData(JSONObject user) {
        this.user = user;
    }

    /**
     * 加载的状态
     *
     * @param loadingState
     */
    public void setLoadingState(int loadingState) {
        this.loadingState = loadingState;
        notifyDataSetChanged();
    }


    public class MyViewHolder extends RecyclerView.ViewHolder {
        View view;
        @Bind(R.id.tv_dateline)
        TextView mTvDateline;
        @Bind(R.id.tv_orderId)
        TextView mTvOrderId;
        @Bind(R.id.tv_phone)
        TextView mTvPhone;
        @Bind(R.id.tv_money)
        TextView mTvMoney;

        public MyViewHolder(View itemView) {
            super(itemView);
            this.view = itemView;
            ButterKnife.bind(this, itemView);
        }
    }

    public class FooterViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.ll_loading)
        CustomLinearlayout mLoading;
        @Bind(R.id.tv_loadcomplent)
        TextView mLoadComplent;

        public FooterViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
